package cn.ylz.ylmall.account.service.impl;

import cn.ylz.ylmall.account.dao.persist.mapper.AddressMapper;
import cn.ylz.ylmall.account.pojo.entity.UserAddress;
import cn.ylz.ylmall.account.pojo.param.AddressAddnewParam;
import cn.ylz.ylmall.account.pojo.vo.UserAddressListVO;
import cn.ylz.ylmall.account.service.AddressService;
import cn.ylz.ylmall.common.ex.ServiceException;
import cn.ylz.ylmall.common.web.ServiceCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    private static final int DEFAULT_ADDRESS_FLAG = 1;

    @Override
    public void addNew(AddressAddnewParam param, Long userId) {
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(param, userAddress);
        userAddress.setUserId(userId);

        if (param.getIsDefaultAddress() == DEFAULT_ADDRESS_FLAG) {
            clearOtherDefaultAddresses(userId);
        }

        int insert = addressMapper.insert(userAddress);
        if (insert != 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "添加收货地址失败，请稍后再尝试！");
        }
    }

    private void clearOtherDefaultAddresses(Long userId) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("is_default_address", DEFAULT_ADDRESS_FLAG);

        List<UserAddress> defaultAddresses = addressMapper.selectList(queryWrapper);
        for (UserAddress address : defaultAddresses) {
            address.setIsDefaultAddress(0);
            addressMapper.updateById(address);
        }
    }

    @Override
    public void delete(Long id) {
        int count = addressMapper.deleteById(id);
        if (count < 1) {
            String message = "删除地址失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void update(Long id, AddressAddnewParam param,Long userId) {
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(param,userAddress);

        if (param.getIsDefaultAddress() == DEFAULT_ADDRESS_FLAG) {
            clearOtherDefaultAddresses(userId);
        }
        userAddress.setId(id);
        int rows = addressMapper.updateById(userAddress);
        if (rows != 1) {
            String message = "修改地址信息失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public List<UserAddressListVO> addressListByUserId(Long id) {
        return addressMapper.addressListByUserId(id);
    }

    @Override
    public UserAddressListVO addressByUserId(Long id) {
        return addressMapper.addressByUserId(id);
    }
}
